<template>
	<view class="group-upgrade">
		<nav-header :fixed="false" :show-border="false" :show-right="false" bg-color="transparent">
			<template v-slot:left>
				<view class="nav">
					<uni-icons class="back" type="back" color="#fff" size="20" @tap="onNavBack()"></uni-icons>
				</view>
			</template>
		</nav-header>

		<view class="card">
			<view class="upgrade">
				<view class="title">升级官方群</view>
				<view class="sub">拓展商业伙伴 共建行业楷模 助您事业发展</view>
				<view class="bottom">
					<view>￥498/群起</view>
					<view>
						<button>立即开通</button>
					</view>
				</view>
			</view>
		</view>

		<view style="text-align: center;color: #fff;">官方群尊享独一无二特权</view>

		<view class="pay">
			<view class="content">
				<view class="title">支付方式</view>
				<view class="way">
					<up-cell-group>
						<up-cell @click="selectPayment('wxpay')">
							<template v-slot:icon>
								<view style="display: flex;align-items: center;">
									<svg-icon icon="wxpay" :width="50"></svg-icon>
									<text>微信支付</text>
								</view>
							</template>
							<template v-slot:value>
								<svg-icon v-if="selectedPayment === 'wxpay'" icon="point" :width="25"></svg-icon>
							</template>
						</up-cell>
						<up-cell @click="selectPayment('alipay')">
							<template v-slot:icon>
								<view style="display: flex;align-items: center;">
									<svg-icon icon="alipay" :width="50"></svg-icon>
									<text>支付宝支付</text>
								</view>
							</template>
							<template v-slot:value>
								<svg-icon v-if="selectedPayment === 'alipay'" icon="point" :width="25"></svg-icon>
							</template>
						</up-cell>
						<up-cell @click="selectPayment('balance')">
							<template v-slot:icon>
								<view style="display: flex;align-items: center;">
									<svg-icon icon="balance" :width="50"></svg-icon>
									<text class="balance">余额支付(余额不足)，剩余￥0.00元</text>
								</view>
							</template>
							<template v-slot:value>
								<svg-icon v-if="selectedPayment === 'balance'" icon="point" :width="25"></svg-icon>
							</template>
						</up-cell>
					</up-cell-group>
				</view>
				<view class="footer">
					<!-- #ifdef MP-WEIXIN -->
					<button @click="submitOrder" :disabled="!selectedPayment">￥<text
							style="font-size: 34rpx;font-weight: 600;">498</text> 立即开通</button>
						<!-- #endif -->

					<!-- #ifndef MP-WEIXIN -->
					<button @click="submitOrderApp" :disabled="!selectedPayment">￥<text
							style="font-size: 34rpx;font-weight: 600;">498</text> 立即开通</button>
						<!-- #endif -->
					<view class="privacy">
						<radio value="r1" :checked="checked" @click="changeCheck" color="#573508"
							style="transform: scale(0.8);" />同意平台
						<navigator class="nav-privacy" url="">
							《开通协议》
						</navigator>
						<navigator class="nav-privacy" url="">
							《官方群扩展功能》
						</navigator>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		genOrderNumber
	} from '../../utils';

	export default {
		data() {
			return {
				selectedPayment: "wxpay",
				checked: false, // 协议勾选状态
				balance: 0.00,
				groupId: null,
				group: null
			}
		},
		methods: {
			onNavBack() {
				uni.navigateBack()
			},
			changeCheck() {
				this.checked = !this.checked
			},
			selectPayment(paymentMethod) {
				this.selectedPayment = paymentMethod;
			},
			submitOrderApp(){
				uni.showToast({
					title: '敬请期待',
					icon: 'none'
				});
			},
			async submitOrder() {
				if (!this.checked) {
					uni.showToast({
						title: '请同意平台协议',
						icon: 'none'
					});
					return;
				}
				if (!this.selectedPayment) {
					uni.showToast({
						title: '请选择支付方式',
						icon: 'none'
					});
					return;
				}
				if (this.selectedPayment == 'wxpay') {
					const self = this
					uni.showLoading({
						title: "正在请求资源"
					})

					// console.log("this.groupId",this.groupId)
					// this.$http({
					// 	url: '/company/convertBusiness?groupId=' + this.groupId, // 替换为您的实际 API 接口地址
					// 	method: 'POST',
					// }).then(resp=>{
					// 	uni.showModal({
					// 		title: "支付成功，您的升级订单已进入排队，请耐心等待",
					// 		icon: 'success'
					// 	})
					// 	uni.hideLoading()
					// })	
					wx.getUserInfo({
						//成功后会返回
						success: (userInfoRes) => {
							console.log(userInfoRes);
							wx.login({
								success(loginRes) {
									console.log(loginRes);
									if (loginRes.code) {
										self.$http({
											url: '/pay/wx/getOpenId', // 替换为您的实际 API 接口地址
											method: 'POST',
											data: {
												code: loginRes.code,
											},
										}).then(openidRes => {
											console.log(openidRes);
											uni.showLoading({
												title: "创建订单"
											})
											self.$http({
												url: '/pay/wx/upOfGroup', // 替换为您的实际 API 接口地址
												method: 'POST',
												data: {
													openId: openidRes.openid,
													data: {
														groupId: self.group.id,
													}
												},
											}).then(orderRes => {
												uni.showLoading({
													title: "等待支付"
												})
												wx.requestPayment({
													timeStamp: orderRes
														.timeStamp + "",
													nonceStr: orderRes
														.nonceStr,
													signType: orderRes
														.signType,
													package: orderRes
														.prepayId,
													paySign: orderRes
														.paySign,
													success(res) {
														console.log(res);
														uni.showModal({
															title: "支付成功，您的升级订单已进入排队，请耐心等待",
															icon: 'success'
														})
													},
													// 失败回调
													fail(res) {
														console.log(res);
														uni.showModal({
															title: "支付失败，如您已经支付，请联系客服处理",
															icon: 'fail'
														})
													},
													// 接口调用结束回调
													complete() {
														uni.hideLoading()
													}
												})
											})
										})
									} else {
										console.log('登录失败！' + loginRes.errMsg)
									}
								}
							})
						}
					})
				}

				/* this.$http({
					url: '/company/convertBusiness', // 替换为您的实际 API 接口地址
					method: 'POST',
					data: {
						paymentMethod: this.selectedPayment,
						amount: 460, // 或从您的数据源获取
						balance: this.balance, // 如果需要包含余额
						groupId: this.groupId
					},
				}).then((res) => {
					console.log('订单提交成功:', res);
					uni.showToast({
						icon: 'none',
						title: "提交成功"
					})
					this.goBack()
					this.$http({
						url: `/group/find/${this.groupId}`,
						method: 'GET'
					}).then((group) => {
						const index = this.chatStore.findChatIdx({
							type: "GROUP",
							id: this.groupId
						})
						this.companyStore.loadCompany().then(() => {
							// 重新获取群聊原始数据
							this.chatStore.updateOriginalDataByIdx(index, group)
							getApp().$vm.init()
						})
					})
				}) */
			},
		},
		onLoad(options) {
			this.groupId = options.id;
			this.$http({
				url: `/group/find/${this.groupId}`,
				method: 'GET'
			}).then((group) => {
				console.log(group);
				this.group = group
				console.log(this.group);
			});
		}
	}
</script>

<style lang="less" scoped>
	.group-upgrade {
		width: 100vw;
		height: 100vh;
		background-color: #303132;

		.nav {
			width: 60rpx;
			height: 60rpx;
			line-height: 60rpx;
			background-color: rgba(255, 255, 255, 0.15);
			border-radius: 50%;
			text-align: center;
		}

		.card {
			padding: 30rpx 40rpx;

			.upgrade {
				display: flex;
				flex-direction: column;
				padding: 30rpx;
				border-radius: 30rpx;
				height: 250rpx;
				color: #804614;
				font-size: 30rpx;
				background-image: linear-gradient(to bottom right, #F5D9B1, #E4BC7F);

				.title {
					font-weight: 600;
					font-size: 40rpx;
				}

				.sub {
					margin-top: 10rpx;
				}

				.bottom {
					margin-top: 90rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					button {
						color: #F5D9B1;
						border-radius: 100rpx;
						font-size: 28rpx;
						background-image: linear-gradient(to right, #804614, #573508);
					}
				}
			}
		}

		.pay {
			background-color: #fff;
			position: fixed;
			bottom: 0;
			left: 0;
			width: 100%;
			height: 600rpx;
			border-top-left-radius: 20rpx;
			border-top-right-radius: 20rpx;

			:deep(.u-cell__body) {
				padding: 20rpx 0 !important;
			}

			.content {
				padding: 40rpx;

				.title {
					font-weight: 600;
					font-size: 32rpx;
				}

				.way {
					margin-top: 10rpx;

					text {
						font-size: 28rpx;
						margin-left: 20rpx;
						margin-top: -10rpx;
					}

					.balance {
						color: #7F7F7F;
					}
				}

				.footer {
					width: 90%;
					position: fixed;
					bottom: 30rpx;

					button {
						border: none;
						height: 80rpx;
						line-height: 80rpx;
						color: #573508;
						border-radius: 100rpx;
						font-size: 28rpx;
						background-color: #ECCDA1;
					}

					.privacy {
						display: flex;
						align-items: center;
						justify-content: center;
						margin-top: 10rpx;
						font-size: 28rpx;

						.nav-privacy {
							color: #c2a884;
						}
					}
				}
			}
		}
	}
</style>